package com.lagou.service.impl;

import com.lagou.base.StatusCode;
import com.lagou.dao.CourseDao;
import com.lagou.dao.impl.CourseDaoImpl;
import com.lagou.pojo.Course;
import com.lagou.service.CourseService;
import com.lagou.utils.DateUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 课程模块Service实现类
 */
public class CourseServiceImpl implements CourseService {
    //创建CourseDao
    CourseDao courseDao =new CourseDaoImpl();

    @Override
    public List<Course> findCourseList() {
        return courseDao.findCourseList();
    }

    @Override
    public List<Course> findByCourseNameAndStatus(String courseName, String status) {
        return courseDao.findByCourseNameAndStatus(courseName,status);

    }

    @Override
    public String savaCourseSalasInfo(Course course) {
        //1.补全课程信息
        String strDate = DateUtils.getDateFormart();
        course.setCreate_time(strDate);//创建时间
        course.setUpdate_time(strDate);//修改时间
        course.setStatus(1);//上架
        //2.执行插入操作
        int i = courseDao.savaCourseSalasInfo(course);
        if(i>0){
            return  StatusCode.SUCCESS.toString();

        }else
        {
            return StatusCode.FAIL.toString();
        }
    }

    @Override
    public Course findCourseByid(int id) {
        return courseDao.findCourseByid(id);
    }

    @Override
    public String updateCourseSalesInfo(Course course) {
        int row= courseDao.updateCourseSalesInfo(course);
        if(row>0){
            //保存成功
           return StatusCode.SUCCESS.toString();

        }
        else{
            //保存失败
            return StatusCode.FAIL.toString();
        }
    }

    @Override
    public Map<String, Integer> updateCourseStatus(Course course) {
        int row = courseDao.updateCourseStatus(course);
        Map<String,Integer> map =new HashMap<>();
        if(row>0){

            if(course.getStatus()==0){
                map.put("Status",0);
            }else{
                map.put("Status",1);
            }
        }
        return map;
    }
}
